<template>
	<view class="update">
		<uni-popup ref="popup" type="message" class="tip">
			<uni-popup-message :type="messageType" :message="messageText" :duration="2000" class="message"></uni-popup-message>
		</uni-popup>
		<view class="BG"></view>
		<view class="header">
			<view class="tag" @click="goBack"><</view>
			<view>修改昵称</view>
		</view>
		<view class="box">
			<input type="text" value="" v-model="user.realname" maxlength="20" />
			<button type="primary" size="mini" @click="updateRealname">完成</button>
		</view>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';
	
export default {
	components: {
		uniPopup,
		uniPopupMessage,
		uniPopupDialog
	},
	data() {
		return {
			user: {},
			messageText: '添加成功',
			messageType: 'success',
		};
	},
	mounted() {
		this.user = uni.getStorageSync('usernameInfo') ? uni.getStorageSync('usernameInfo') : {};
	},
	methods: {
		open() {
			this.$refs.popup.open();
		},
		updateRealname() {
			if (this.user.realname.trim() == '') {
				//失败回调
				this.messageText="帅哥(美女)请好好写哈"
				this.messageType="warn"
				this.open()
			} else {
				uni.request({
					method:"POST",
					url: 'https://www.ttzxh.icu:443/api/user/update',
					data: {
						realname: this.user.realname.trim(),
						username: this.user.username
					},
					header: {
						Authorization: uni.getStorageSync('LoginToken')
					},
					success: (res) => {
						if(res.data.error===-1){
							this.messageText="修改成功"
							this.messageType="success"
							this.open()
							uni.setStorageSync("usernameInfo",this.user)
							uni.reLaunch({
								url:"/pages/chat/chat"
							})
						}else{
							this.messageText="额,失败了再试一次吧"
							this.messageType="warn"
							this.open()
						}
					},
					fail: () => {
						this.messageText="出错了,请联系我或稍后再试"
						this.messageType="error"
						this.open()
						this.user.realname = uni.getStorageSync('usernameInfo').realname
					}
				});
			}
		},
		goBack() {
			uni.navigateBack();
		}
	}
};
</script>

<style lang="scss" scoped>
.update {
	display: flex;
	justify-content: center;
	height: 100vh;
}
.BG {
	position: fixed;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
	background-image: url(../../static/img/bingqishi.jpg);
	background-size: cover;
	opacity: 0.3;
	margin: auto;
	z-index: -1;
}
.header {
	display: flex;
	justify-content: center;
	align-items: center;
	position: fixed;
	top: 0;
	width: 100%;
	height: 50px;
	box-shadow: 0 0 2px 0px #808080;
	.tag {
		position: absolute;
		left: 20px;
		&:active {
			color: $uni-text-select-color;
		}
	}
}
.box {
	border-radius: 10px;
	box-sizing: border-box;
	margin: 80px 0px;
	display: flex;
	align-items: center;
	width: 700upx;
	height: 50px;
	background-color: #ffffff;
	input {
		box-sizing: border-box;
		width: 500upx;
		height: 30px;
		padding: 5px 15px;
	}
	button {
		width: 70px;
	}
}
</style>
